package com.koron.car.web.authorized.controller;

import com.alibaba.fastjson.JSON;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.koron.bean.base.Response;
import com.koron.car.web.authorized.bean.entity.OperateControl;
import com.koron.car.web.authorized.bean.query.OperateControlQuery;
import com.koron.car.web.authorized.bean.vo.OperateControlVO;
import com.koron.car.web.authorized.service.OperateControlService;
import com.koron.common.core.config.ThreadLocalContext;
import com.koron.common.core.interceptor.OrderByInterceptor;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.koron.ebs.mybatis.ADOConnection;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * (WCmOperateControl)表控制层
 *
 * @author twcao
 * @since 2022-03-30 16:48:32
 */
@Slf4j
@RestController
@RequestMapping("/operateControl")
@Api(tags = "车辆编制-编制概况")
public class OperateControlController {

    /**
     * 服务对象
     */
    @Resource
    private OperateControlService operateControlService;

    @GetMapping("/selectOne")
    @ApiOperation("根据id查询信息")
    @ApiImplicitParam(name = "id", value = "概况id", required = true)
    public Response<OperateControl> selectOne(@RequestParam String id) {
        log.info("根据id查询信息， id = {}", id);
        OperateControl bean = ADOConnection.runTask(ThreadLocalContext.get().getCurrDs(), operateControlService, "queryById", OperateControl.class, id);
        return Response.ok(bean);
    }

    @GetMapping("/getOperateControl")
    @ApiOperation("获取当前水司的运营情况")
    public Response<OperateControl> getOperateControl(@RequestParam(required = false) String applyId) {
        OperateControl bean = null;
        if(StringUtils.isBlank(applyId)) {
            bean = ADOConnection.runTask(ThreadLocalContext.get().getCurrDs(), operateControlService, "queryByMark", OperateControl.class);
        } else {
            bean = ADOConnection.runTask(ThreadLocalContext.get().getCurrDs(), operateControlService, "queryByApplyId", OperateControl.class, applyId);
        }
        return Response.ok(bean);
    }

    @PostMapping("/saveOrUpdate")
    @ApiOperation("新增或更新概况")
    public Response<OperateControlVO> saveOrUpdate(@RequestBody OperateControlVO oc) {
        log.info("新增或更新概况， oc = {}", JSON.toJSONString(oc));
        OperateControlVO bean = ADOConnection.runTask(ThreadLocalContext.get().getCurrDs(), operateControlService, "saveOrUpdate", OperateControlVO.class, oc);
        return Response.ok(bean);
    }

    @GetMapping("/queryList")
    @ApiOperation("概况分页查询")
    public Response<PageInfo<OperateControl>> queryList(OperateControlQuery query) {
        OrderByInterceptor.setOrderBy(query.getOrders());
        PageHelper.startPage(query.getPage(), query.getPageSize());
        log.info("概况分页查询， query = {}", JSON.toJSONString(query));
        List<OperateControl> beans = ADOConnection.runTask(ThreadLocalContext.get().getCurrDs(), operateControlService, "queryList", List.class, query);
        return Response.ok(new PageInfo<>(beans));
    }

    @GetMapping("/delete")
    @ApiOperation("根据id删除概况")
    public Response<Boolean> delete(@RequestParam String id) {
        log.info("根据id删除概况， id = {}", id);
        Boolean flag = ADOConnection.runTask(ThreadLocalContext.get().getCurrDs(), operateControlService, "deleteById", Boolean.class, id);
        return Response.ok(flag);
    }
}
